package com.zxmr.hospital.customer.action;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.domain.Customer;
import com.zxmr.domain.service.CustomerSearchBean;
import com.zxmr.domain.service.CustomerService;
import com.zxmr.hospital.customer.dto.AllCustomerDTO;
import com.zxmr.hospital.customer.dto.JSONListDTO;
import com.zxmr.navigate.Executor;

public class QueryAllCustomer extends Executor{

	@Override
	public void execute(Request request, Response response) {
		
		
		CustomerService service = new CustomerService();
		int count = getCountPerPage(request);
		int start = getCountStart(request);
		String value = request.getRequestParameters().get("value");
		String location = request.getRequestParameters().get("location");
		String items = request.getRequestParameters().get("items");
		String type = request.getRequestParameters().get("type");
		
		CustomerSearchBean searchBean = new CustomerSearchBean();
		searchBean.setCompany(request.getAccount().getCompany());
		searchBean.setLimit(count);
		searchBean.setLocation(location);
		searchBean.setStart(start);
		searchBean.setValue(value);
		searchBean.setItems(items);
		searchBean.setType(type);
		
		List<Customer> customers = service.queryCustomerPagination(searchBean);
		int total = service.queryCustomerCount(searchBean);
		
		JSONListDTO dto = convertCustomerToDTO(customers,total);
		reply(response,dto);
	}
	
	private int getCountPerPage(Request request)
	{
		String count = request.getRequestParameters().get("size");
		if(count == null || "".equals(count.trim()))return 10;
		try
		{
			return Integer.parseInt(count);
		}catch(Exception e)
		{
			e.printStackTrace();
			return 10;
		}
	}
	private int getCountStart(Request request)
	{
		String start = request.getRequestParameters().get("start");
		if(start == null || "".equals(start.trim()))return 0;
		try
		{
			return Integer.parseInt(start);
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}
	private void reply(Response response,JSONListDTO dto)
	{
		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(dto));
		response.setPayload(payload);
	}
	private JSONListDTO convertCustomerToDTO(List<Customer> customers,int total)
	{
		if(customers == null)return null;
		JSONListDTO list = new JSONListDTO();
		List<AllCustomerDTO> rows = new ArrayList<AllCustomerDTO>();
		
		for(Customer customer : customers)
		{
			AllCustomerDTO dto = customerToDTO(customer);
			rows.add(dto);
		}
		list.setRows(rows);
		list.setTotal(total);
		return list;
	}
	private AllCustomerDTO customerToDTO(Customer customer)
	{
		AllCustomerDTO dto = new AllCustomerDTO();
		dto.setAge(customer.getAge());
		dto.setCell(customer.getCell());
		dto.setCity(customer.getCity());
		dto.setId(customer.getId());
		dto.setImage(customer.getImage());
		dto.setName(customer.getName());
		dto.setProvince(customer.getProvince());
		dto.setQq(customer.getQq());
		dto.setWeixin(customer.getWeixin());
		dto.setSex(customer.getSex());
		dto.setWork(customer.getWork());
		dto.setEmail(customer.getEmail());
		dto.setCustomerType(customer.getCustomerType());
		dto.setItems(customer.getItems());
		
		return dto;
	}
}
